From a89e4d770c0c2cece5c5cf4b1e93c8983a6b452f Mon Sep 17 00:00:00 2001 From: "smh22@firebug.cl.cam.ac.uk" Date: Tue, 2 Aug 2005 09:31:47 +0000 Subject: [PATCH] The attached patch adds the support that was only stubbed in to be able to support having reading the boot loader config when you're using a whole disk (eg, file:/root/disk.img,xvda,w) as opposed to just a partition. This reads the partition table in the MBR to find the active partition and then passes that offset down into the filesystem reading code. Signed-off-by: Jeremy Katz --- tools/pygrub/setup.py | 2 +- tools/pygrub/src/fsys/ext2/__init__.py | 2 +- tools/pygrub/src/fsys/ext2/ext2module.c | 31 +++++++++++++--------- tools/pygrub/src/pygrub | 34 +++++++++++++++++++------ 4 files changed, 47 insertions(+), 22 deletions(-) diff --git a/tools/pygrub/setup.py b/tools/pygrub/setup.py index b72ea38857..6b32346b06 100644 --- a/tools/pygrub/setup.py +++ b/tools/pygrub/setup.py @@ -23,7 +23,7 @@ if os.path.exists("/usr/include/reiserfs/reiserfs.h"): fsys_pkgs.append("grub.fsys.reiser") setup(name='pygrub', - version='0.2', + version='0.3', description='Boot loader that looks a lot like grub for Xen', author='Jeremy Katz', author_email='katzj@redhat.com', diff --git a/tools/pygrub/src/fsys/ext2/__init__.py b/tools/pygrub/src/fsys/ext2/__init__.py index ff8f7af48f..4c3fe12c00 100644 --- a/tools/pygrub/src/fsys/ext2/__init__.py +++ b/tools/pygrub/src/fsys/ext2/__init__.py @@ -32,7 +32,7 @@ class Ext2FileSystemType(FileSystemType): def open_fs(self, fn, offset = 0): if not self.sniff_magic(fn, offset): raise ValueError, "Not an ext2 filesystem" - return Ext2Fs(fn) + return Ext2Fs(fn, offset = offset) register_fstype(Ext2FileSystemType()) diff --git a/tools/pygrub/src/fsys/ext2/ext2module.c b/tools/pygrub/src/fsys/ext2/ext2module.c index bef4bb6f9f..7b81d8bab7 100644 --- a/tools/pygrub/src/fsys/ext2/ext2module.c +++ b/tools/pygrub/src/fsys/ext2/ext2module.c @@ -208,22 +208,28 @@ static PyObject * ext2_fs_open (Ext2Fs *fs, PyObject *args, PyObject *kwargs) { static char *kwlist[] = { "name", "flags", "superblock", - "block_size", NULL }; + "block_size", "offset", NULL }; char * name; - int flags = 0, superblock = 0, err; + int flags = 0, superblock = 0, offset = 0, err; unsigned int block_size = 0; ext2_filsys efs; + char offsetopt[30]; - if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s|iii", kwlist, - &name, &flags, &superblock, &block_size)) - return NULL; + if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s|iiii", kwlist, + &name, &flags, &superblock, + &block_size, &offset)) + return NULL; if (fs->fs != NULL) { PyErr_SetString(PyExc_ValueError, "already have an fs object"); return NULL; } - err = ext2fs_open(name, flags, superblock, block_size, + if (offset != 0) { + snprintf(offsetopt, 29, "offset=%d", offset); + } + + err = ext2fs_open2(name, offsetopt, flags, superblock, block_size, unix_io_manager, &efs); if (err) { PyErr_SetString(PyExc_ValueError, "unable to open file"); @@ -323,14 +329,15 @@ static PyObject * ext2_fs_new(PyObject *o, PyObject *args, PyObject *kwargs) { static char *kwlist[] = { "name", "flags", "superblock", - "block_size", NULL }; + "block_size", "offset", NULL }; char * name; - int flags = 0, superblock = 0; + int flags = 0, superblock = 0, offset; unsigned int block_size = 0; Ext2Fs *pfs; - if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s|iii", kwlist, - &name, &flags, &superblock, &block_size)) + if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s|iiii", kwlist, + &name, &flags, &superblock, &block_size, + &offset)) return NULL; pfs = (Ext2Fs *) PyObject_NEW(Ext2Fs, &Ext2FsType); @@ -339,8 +346,8 @@ ext2_fs_new(PyObject *o, PyObject *args, PyObject *kwargs) pfs->fs = NULL; if (!ext2_fs_open(pfs, - Py_BuildValue("siii", name, flags, superblock, block_size), - NULL)) + Py_BuildValue("siiii", name, flags, superblock, + block_size, offset), NULL)) return NULL; return (PyObject *)pfs; diff --git a/tools/pygrub/src/pygrub b/tools/pygrub/src/pygrub index 545feea54f..765f54c71e 100644 --- a/tools/pygrub/src/pygrub +++ b/tools/pygrub/src/pygrub @@ -24,7 +24,7 @@ sys.path = [ '/usr/lib/python' ] + sys.path import grub.GrubConf import grub.fsys -PYGRUB_VER = 0.02 +PYGRUB_VER = 0.3 def draw_window(): @@ -77,24 +77,39 @@ def is_disk_image(file): buf = os.read(fd, 512) os.close(fd) - if len(buf) >= 512 and struct.unpack("H", buf[0x1fe: 0x200]) == (0xaaff): + if len(buf) >= 512 and struct.unpack("H", buf[0x1fe: 0x200]) == (0xaa55,): return True return False +SECTOR_SIZE=512 +def get_active_offset(file): + """Find the offset for the start of the first active partition in the + disk image file.""" + fd = os.open(file, os.O_RDONLY) + buf = os.read(fd, 512) + for poff in (446, 462, 478, 494): # partition offsets + # active partition has 0x80 as the first byte + if struct.unpack("